using HCManage.Api.Models;
using HCManage.IBll;
using HCManage.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HCManage.Api.Controllers.Settings
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        #region 模块

        private readonly ISysUserBll userBll;

        private DataResultModel dataResult = new DataResultModel();

        #endregion

        #region 构造函数

        public UserController(ISysUserBll user_bll)
        {
            this.userBll = user_bll;
        }

        #endregion

        [HttpGet("{id}")]
        public JsonResult Get(string id)
        {
            sys_user model = userBll.Get(id);
            dataResult.success = true;
            dataResult.data = model;
            return new JsonResult(dataResult);
        }

        [HttpPost]
        public JsonResult Post(sys_user model)
        {
            bool success = userBll.Insert(model);
            if (!success)
            {
                throw new Exception("新增失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        [HttpPut]
        public JsonResult Put(sys_user model)
        {
            bool success = userBll.Update(model);
            if (!success)
            {
                throw new Exception("更新失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        [HttpDelete]
        public JsonResult Delete(string id)
        {
            bool success = userBll.Delete(id, id);
            if (!success)
            {
                throw new Exception("删除失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }
    }
}
